/**
 * Order路由模块
 */

/**
 * @swagger
 * tags:
 *   - name: 订单路由
 *     description: 订单管理相关操作
 *
 * /orders:
 *   post:
 *     summary: 创建新订单
 *     tags: [Orders]
 *     operationId: createOrder
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/Order'
 *     responses:
 *       '201':
 *         description: 订单创建成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Order'
 *
 * /orders/course:
 *   get:
 *     summary: 获取指定课程的所有订单
 *     tags: [Orders]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: courseId
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 课程ID
 *     responses:
 *       '200':
 *         description: 成功返回订单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Order'
 *
 * /orders/user:
 *   get:
 *     summary: 获取指定用户的订单列表
 *     tags: [Orders]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 用户ID
 *     responses:
 *       '200':
 *         description: 成功返回订单列表
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Order'
 *
 * /orders/{id}:
 *   get:
 *     summary: 获取指定ID的订单详情
 *     tags: [Orders]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 订单ID
 *     responses:
 *       '200':
 *         description: 成功返回订单详情
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Order'
 *       '404':
 *         description: 订单未找到
 *
 *   delete:
 *     summary: 删除指定ID的订单
 *     tags: [Orders]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 订单ID
 *     responses:
 *       '204':
 *         description: 订单删除成功
 *       '404':
 *         description: 订单未找到
 *
 * securitySchemes:
 *   bearerAuth:
 *     type: http
 *     scheme: bearer
 *     bearerFormat: JWT
 *
 * components:
 *   schemas:
 *     Order:
 *       type: object
 *       required:
 *         - id
 *         - userId
 *         - courseId
 *         - orderTotal
 *         - orderTime
 *       properties:
 *         id:
 *           type: integer
 *           format: int64
 *           example: 100
 *           description: 订单ID
 *         userId:
 *           type: integer
 *           format: int64
 *           description: 用户ID
 *         courseId:
 *           type: integer
 *           format: int64
 *           description: 课程ID
 *         orderTotal:
 *           type: number
 *           format: decimal
 *           description: 订单总金额
 *         orderTime:
 *           type: string
 *           format: date-time
 *           description: 下单时间
 *         transactionId:
 *           type: string
 *           description: 交易ID (可选字段)
 *         # 可根据实际情况添加其他属性
 */

// 引入所需模块
var express = require('express');
var router = express.Router();

// 引入课程相关控制器
const order = require("../controller/order");

const auth = require("../controller/auth");

//守卫路由：所有路由必须执行权限控制的判断
router.all("*", auth.isLogin);

/**
 * @swagger
 * /orders:
 *   post:
 *     tags:
 *       - 订单路由
 *     summary: 新增订单信息
 *     description: 添加新的订单信息并返回新增订单的ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/OrserSchema"
 *     security:
 *       - BearerAuth: []
 *     responses:
 *       200:
 *         description: "请求成功，返回新增订单的ID"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   format: int64
 *                   description: 新增订单的ID
 */
router.post("/", order.add);

/**
 * @swagger
 * /orders/countAll:
 *   get:
 *     tags:
 *       - 订单路由
 *     summary: 获取所有的订单总数
 *     description: 返回系统中指定用户ID的订单总数
 *     responses:
 *       200:
 *         description: "请求成功，返回指定用户ID的订单总数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 订单总数
 */
router.get("/countAll", auth.isAdmin, order.getCountAll);

/**
 * @swagger
 * /orders/count:
 *   get:
 *     tags:
 *       - 订单路由
 *     summary: 获取指定用户ID的订单总数
 *     description: 返回系统中指定用户ID的订单总数
 *     responses:
 *       200:
 *         description: "请求成功，返回指定用户ID的订单总数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 订单总数
 */
router.get("/count", auth.isAdmin, order.getCount);

/**
 * @swagger
 * /orders/course:
 *   get:
 *     tags:
 *       - 订单路由
 *     summary: 获取所有的订单列表信息
 *     description: 返回系统中的该用户的订单信息，可按需指定查询数量和偏移量
 *     parameters:
 *       - name: limit
 *         in: query
 *         description: 返回结果的数量限制，默认返回全部
 *         required: false
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 结果集起始位置偏移量，默认为0
 *         required: false
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: "请求成功，返回用户的订单信息"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/OrderSchema'
 */
router.get("/all",auth.isAdmin,order.findAll);

/**
 * @swagger
 * /orders/course:
 *   get:
 *     tags:
 *       - 订单路由
 *     summary: 获取指定课程ID的订单列表信息
 *     description: 返回系统中的该用户的订单信息，可按需指定查询数量和偏移量
 *     parameters:
 *       - name: limit
 *         in: query
 *         description: 返回结果的数量限制，默认返回全部
 *         required: false
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 结果集起始位置偏移量，默认为0
 *         required: false
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: "请求成功，返回用户的订单信息"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/OrderSchema'
 */
router.get("/course",order.findAllByCourseId);

/**
 * @swagger
 * /orders/user:
 *   get:
 *     tags:
 *       - 订单路由
 *     summary: 获取指定的用户ID的订单列表信息
 *     description: 返回系统中的该用户的订单信息，可按需指定查询数量和偏移量
 *     parameters:
 *       - name: limit
 *         in: query
 *         description: 返回结果的数量限制，默认返回全部
 *         required: false
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 结果集起始位置偏移量，默认为0
 *         required: false
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: "请求成功，返回用户的订单信息"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/OrderSchema'
 */
router.get("/user",auth.isAdmin, order.findAllByUserId);

/**
 * @swagger
 * /orders/{id}:
 *   get:
 *     tags:
 *       - 订单路由
 *     summary: 获取指定订单ID的订单信息
 *     description: 根据订单内部ID返回订单详细信息
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 要查询的订单的内部ID
 *     responses:
 *       200:
 *         description: "请求成功，返回订单信息"
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/OrderSchema'
 */
router.get("/:id", auth.isAdmin, order.find);


/**
 * @swagger
 * /orders/{id}:
 *   delete:
 *     tags:
 *       - 订单路由
 *     summary: 删除指定订单信息
 *     description: 根据订单的内部ID删除订单信息，并返回受影响的记录
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 待删除订单的内部ID
 *     responses:
 *       200:
 *         description: "请求成功，返回受影响的记录数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.delete("/:id", auth.isAdmin, order.remove);


module.exports = router;